package com.cskaoyan.service;

import com.cskaoyan.bean.BaseParam;

import com.cskaoyan.bean.user.Feedback;
import com.cskaoyan.bean.user.FeedbackExample;
import com.cskaoyan.bean.BaseData;

import com.cskaoyan.bean.user.FeedbackVo;
import com.cskaoyan.bean.wx_feedback.FeedbackBo;
import com.cskaoyan.mapper.FeedbackMapper;
import com.cskaoyan.util.DateUtils;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * \* Description:
 * \
 */
@Service
public class FeedBackServiceImpl implements FeedbackService {

    @Autowired
    FeedbackMapper feedbackMapper;

    @SuppressWarnings("all")
    @Override
    public BaseData<FeedbackVo> feedback(BaseParam param, String username, Integer id) {
        PageUtil.start(param);
        FeedbackExample feedbackExample = new FeedbackExample();
        feedbackExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (username != null && !"".equals(username))
            criteria.andUsernameLike("%" + username + "%");
        if (id != null && !"".equals(id))
            criteria.andIdEqualTo(id);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);

        ArrayList<FeedbackVo> feedbackVos = new ArrayList<>();
        //修改Date
        for (Feedback feedback : feedbacks) {

            FeedbackVo feedbackVo = new FeedbackVo(feedback);

            feedbackVos.add(feedbackVo);
        }
        PageInfo<FeedbackVo> feedbackPageInfo = new PageInfo<>(feedbackVos);
        BaseData<FeedbackVo> feedbackBaseData = new BaseData<>(feedbackPageInfo, feedbackVos);
        return feedbackBaseData;
    }

    @Override
    public void submitFeedback(FeedbackBo feedbackBo) {
        Date date = new Date(System.currentTimeMillis());
        Subject subject = SecurityUtils.getSubject();
        String primaryPrincipal = (String) subject.getPrincipals().getPrimaryPrincipal();
        int id = feedbackMapper.getIdByPrincipal(primaryPrincipal);
        Feedback feedback = new Feedback();

        feedback.setAddTime(date);
        feedback.setUserId(id);
        feedback.setMobile(feedbackBo.getMobile());
        feedback.setFeedType(feedbackBo.getFeedType());
        feedback.setContent(feedbackBo.getContent());


        if (feedbackBo.isHasPicture()) {

            String[] picUrls = feedbackBo.getPicUrls();
            feedback.setPicUrls(picUrls);
            feedback.setHasPicture(true);
        } else {
            feedback.setHasPicture(false);

        }
        feedback.setStatus(0);

        feedbackMapper.insertSelective(feedback);
    }
}
